package com.xiezhr.pbideaapitestdemo.controller;

import com.xiezhr.pbideaapitestdemo.entity.ApiResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: pbideaApiTestDemo
 * @description:
 * @author: xie.zhr
 * @create: 2024-12-07 11:22
 **/
@RestController
@RequestMapping("/upload")
public class FileUploadController {
    private static final String UPLOAD_DIR = "uploads/";

    @PostMapping("/singleFile")
    public ApiResponse<List<String>> uploadFile(@RequestParam("file") MultipartFile file) {

        // 获取当前工作目录
        Path currentPath = Paths.get("").toAbsolutePath();
        // 创建uploads目录的完整路径
        File uploadDir = new File(currentPath.toString(), UPLOAD_DIR);
        if (!uploadDir.exists()) {

            // 创建目录
            boolean created = uploadDir.mkdirs();
            if (!created) {
                return new ApiResponse<>(500, "无法创建上传目录.", null);
            }

        }
        List<String> successFiles = new ArrayList<>();
        List<String> failedFiles = new ArrayList<>();

        // 获取文件名
        String fileName = file.getOriginalFilename();
        File destinationFile = new File(uploadDir, fileName);

        try {
            // 保存文件
            file.transferTo(destinationFile);
            successFiles.add(fileName);

        } catch (IOException e) {
            e.printStackTrace();
            failedFiles.add(fileName + " - " + e.getMessage());
        }

        String message = "上传成功文件: " + successFiles.size() + ", 失败文件: " + failedFiles.size();
        return new ApiResponse<>(200, message, successFiles);
    }

    @PostMapping("/mulFiles")
    public ApiResponse<List<String>> uploadFiles(@RequestParam("fileList") MultipartFile[] files) {
        // 获取当前工作目录
        Path currentPath = Paths.get("").toAbsolutePath();
        // 创建uploads目录的完整路径
        File uploadDir = new File(currentPath.toString(), UPLOAD_DIR);

        // 如果uploads目录不存在，则创建
        if (!uploadDir.exists()) {
            // 创建目录
            boolean created = uploadDir.mkdirs();
            if (!created) {
                return new ApiResponse<>(500, "无法创建上传目录.", null);
            }
        }

        List<String> successFiles = new ArrayList<>();
        List<String> failedFiles = new ArrayList<>();

        for (MultipartFile file : files) {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            File destinationFile = new File(uploadDir, fileName);

            try {
                // 保存文件
                file.transferTo(destinationFile);
                successFiles.add(fileName);
            } catch (IOException e) {
                e.printStackTrace();
                failedFiles.add(fileName + " - " + e.getMessage());
            }
        }

        String message = "上传成功文件: " + successFiles.size() + ", 失败文件: " + failedFiles.size();
        return new ApiResponse<>(200, message, successFiles);
    }
}
